
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import rw.ac.kiosc.jdbc.school.School;
import rw.ac.kiosc.jdbc.school.orm.User;

public class Simple extends HttpServlet {

    int accesses = 0;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        School school = new School();
               HttpSession session = request.getSession(false);
        User user = null;
        String userIp = (String) session.getAttribute("userIp");
        int id = 0;
        String userId = (String) session.getAttribute("userId");
        if (userIp != null && userId != null) {
            try {
                id = Integer.parseInt(userId);
            } catch (Exception ex) {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
            if (userIp.equals(request.getRemoteAddr())) {
                user = school.getUserDAO().getUserById(id);
                if (user == null) {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
            } else {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
        } else {
            session.invalidate();
            response.sendRedirect("login.htm");
        }
        accesses++;
        response.setContentType("text/xml");
        //response.getWriter().println("Number of times this servlet has been accessed:" +
        //accesses +
        //" Your Name=" +
        //school.getStudentDAO().
        //getStudentByRegistrationNumber(request.getParameter("gs")).
        //getName
        try {
            response.getOutputStream().write(school.getSchoolByteArrayStream(school).toByteArray());
        } catch (Exception ex) {
            response.getOutputStream().write(("<exception>" + ex.toString() + "</exception>").getBytes());
        }
    }
} 